package com.lazycat.backend.service.impl.chat;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.lazycat.backend.mapper.ChatGroupMapper;
import com.lazycat.backend.mapper.GroupMessageMapper;
import com.lazycat.backend.mapper.UserGroupRelationMapper;
import com.lazycat.backend.pojo.ChatGroup;
import com.lazycat.backend.pojo.GroupMessages;
import com.lazycat.backend.pojo.UserGroupRelation;
import com.lazycat.backend.service.chat.GetRelationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import java.sql.Array;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@Service
public class GetRelationServiceImpl implements GetRelationService {
    @Autowired
    private UserGroupRelationMapper userGroupRelationMapper;

    @Autowired
    private ChatGroupMapper chatGroupMapper;

    @Autowired
    private GroupMessageMapper groupMessageMapper;

    @Override
    public HashMap<String, Object> get(Integer userId) {
        QueryWrapper<UserGroupRelation> userGroupRelationQueryWrapper = new QueryWrapper<>();
        userGroupRelationQueryWrapper.eq("user_id", userId);
        List<UserGroupRelation> userGroupRelations = userGroupRelationMapper.selectList(userGroupRelationQueryWrapper);
        HashMap<String, Object> resp = new HashMap<>();
        ArrayList<JSONObject> items = new ArrayList<>();
        for (UserGroupRelation userGroupRelation : userGroupRelations) {
            JSONObject item = new JSONObject();
            item.put("group_id", userGroupRelation.getGroupId());
            item.put("user_id", userGroupRelation.getUserId());
            QueryWrapper<ChatGroup> chatGroupQueryWrapper = new QueryWrapper<>();
            chatGroupQueryWrapper.eq("group_id", userGroupRelation.getGroupId());
            ChatGroup chatGroup = chatGroupMapper.selectOne(chatGroupQueryWrapper);
            item.put("group_name", chatGroup.getGroupName());
            QueryWrapper<GroupMessages> groupMessagesQueryWrapper = new QueryWrapper<>();
            groupMessagesQueryWrapper.eq("group_id", userGroupRelation.getGroupId());
            List<GroupMessages> groupMessages = groupMessageMapper.selectList(groupMessagesQueryWrapper);
            if (groupMessages == null || groupMessages.size() == 0) {
                item.put("last_message_state","no message");
                item.put("last_message", "there is nothing here");
            } else {
                item.put("last_message_state","success");
                item.put("last_message", groupMessages.get(groupMessages.size() - 1).getMessage());
                item.put("last_message_time", groupMessages.get(groupMessages.size() - 1).getTime());
            }
            items.add(item);
        }
        resp.put("error_message", "success");
        resp.put("relations", items);
        return resp;

    }
}
